#ifndef BIGQ_H
#define BIGQ_H
#include <pthread.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include "Pipe.h"
#include "File.h"
#include "Record.h"

using namespace std;

class PQRecord{
public:
	int runNumber;
	Record* myRec;
	PQRecord(){};
	PQRecord(int rn,Record* rec);
};

class Run{
private:
	Page *page;
	File *file;
	long offset;
	int currentPage;
	int totalPages;

public:
	Run(int totalPg,int off,File* fl);
	int Load();
	int GetNextRecord(Record*);
};

class MergeCompare{
	OrderMaker* ord;
	ComparisonEngine* comeng;
public:
	MergeCompare(OrderMaker* o){
		ord = o;
		comeng = new ComparisonEngine();
	}
	bool operator()(const PQRecord& lhs, const PQRecord& rhs) const;
};

class SortCompare{
	OrderMaker* ord;
	ComparisonEngine* comeng;
public:
	SortCompare(OrderMaker* o){
		ord = o;
		comeng = new ComparisonEngine();
	}
	bool operator()(Record* lhs, Record* rhs) const;
};


class BigQ {
private:
	static int count; 
	Page *tmpPages;
	Page pageToFile; 
	Record **tmpRecordAr;
	Pipe* in;
        Pipe* out;
	File tmpFile;
	vector <Run*> runVector; 
	vector <Record*> tmpRecordVector;
	OrderMaker ordMaker;
	pthread_t thread;
	string fileName;
	long pageToAdd;
	int currentPage;
	int runLength;
	int numberRuns;
	void sortRunLength(int);
	void writeRecordsToFile(int);
	void doMerge();

public:
	BigQ (Pipe &in, Pipe &out, OrderMaker &sortorder, int runlen);
	void doSort();
	//BigQ (Pipe &in, Pipe &out, OrderMaker &sortorder, int runlen, Schema*);
	~BigQ ();
};

#endif
